using System;
using System.Collections.Generic;
using jQueryApi;
using System.Html;

namespace TuringMachine
{
   public class Main
   {
      static int m_iCredits;

      static Div PlaceHolderMenu;
      static Div PlaceHolderBoard;
      static Div ParentBoard;
      static Div PlaceHolderShop;
      static List<string> m_lChallengesCompleted = new List<string>();
      static Upgrades m_lUpgradesBought;
      static Challenge m_CurrentChallenge = null;

      public static void StartApp()
      {
         Challenge.CreateChallenges();
         ReadConfig();

         jQueryObject page = jQuery.FromElement(Document.Body);
         PlaceHolderMenu = new Div(page, "Menu");
         ParentBoard = new Div(page, "Board");         
         PlaceHolderShop = new Div(page, "Shop");
         Clear();

         ShowMainMenu();
      }

      private static void ReadConfig()
      {
         if (Window.LocalStorage["TM_Credits"] == null)
            Window.LocalStorage.SetItem("TM_Credits", 0);
         if (Window.LocalStorage["TM_ChallengesCompleted"] == null)
            Window.LocalStorage.SetItem("TM_ChallengesCompleted", "");
         if (Window.LocalStorage["TM_UpgradesBought"] == null)
            Window.LocalStorage.SetItem("TM_UpgradesBought", "");

         m_iCredits = (int)Window.LocalStorage["TM_Credits"];
         m_lChallengesCompleted.AddRange(((string)Window.LocalStorage["TM_ChallengesCompleted"]).Split(','));
         m_lUpgradesBought = new Upgrades((string)Window.LocalStorage["TM_UpgradesBought"]);
      }

      private static void ShowMainMenu()
      {
         MainMenu MainMenu = new MainMenu(PlaceHolderMenu.pObject, m_iCredits, Challenge.ChallengeList, m_lChallengesCompleted);
         MainMenu.Show();
         MainMenu.OnClose += new DialogEventHandler(MainMenu_OnClose);
         MainMenu.OnLevelClicked += new LevelClickedHandler(MainMenu_OnLevelClicked);
      }

      static void MainMenu_OnLevelClicked(string levelKey)
      {
         Clear();
         ShowBoard(levelKey);
      }

      private static void ShowShop()
      {
         Shop shop = new Shop(PlaceHolderShop.pObject, m_iCredits, UpgradeList.m_lAllUpgrades, m_lUpgradesBought);
         shop.OnClose += new DialogEventHandler(shop_OnClose);
         shop.Show();
      }

      private static void ShowBoard(string key)
      {
         int index = -1;
         for (int i = 0; i < Challenge.ChallengeList.Count; i++)
            if (Challenge.ChallengeList[i].pName == key)
            {
               m_CurrentChallenge = Challenge.ChallengeList[i];
               index = i; break;
            }
         StateBoard sb = new StateBoard(PlaceHolderBoard.pObject, Challenge.ChallengeList[index], m_lUpgradesBought);
         sb.OnLevelCompleted += new EventHandler(sb_OnLevelCompleted);
         sb.OnLevelAbandoned += new EventHandler(sb_OnLevelAbandoned);
      }

      static void sb_OnLevelAbandoned(object sender, EventArgs e)
      {
         Clear();
         ShowMainMenu();  
      }

      static void sb_OnLevelCompleted(object sender, EventArgs e)
      {
         Clear();
         if (!m_lChallengesCompleted.Contains(m_CurrentChallenge.pName))
         {
            m_lChallengesCompleted.Add(m_CurrentChallenge.pName);
            string levelString = (string)Window.LocalStorage["TM_ChallengesCompleted"];
            if (levelString != "") levelString += ",";
            m_iCredits = int.Parse(m_iCredits.ToString()) + int.Parse(m_CurrentChallenge.pCredits.ToString());
            Window.LocalStorage.SetItem("TM_Credits", m_iCredits);
            Window.LocalStorage.SetItem("TM_ChallengesCompleted", levelString + m_CurrentChallenge.pName);
            ReadConfig();
         }
         ShowMainMenu();
      }

      static void shop_OnClose(MessageBoxBase dlg, bool bOkPressed)
      {
         Clear();
         if (bOkPressed) ReadConfig();
         ShowMainMenu();
      }

      static void MainMenu_OnClose(MessageBoxBase dlg, bool bOkPressed)
      {
         Clear();
         if (bOkPressed)
            ShowShop();
         else
         {
            Tutorial t = new Tutorial(PlaceHolderMenu.pObject);
            t.DialogClosed += new EventHandler(t_DialogClosed);
         }
      }

      static void t_DialogClosed(object sender, EventArgs e)
      {
         Clear();
         ShowMainMenu();
      }

      private static void Clear()
      {
         PlaceHolderMenu.pObject.Empty();
         ParentBoard.pObject.Empty();
         PlaceHolderShop.pObject.Empty();
         PlaceHolderBoard = new Div(ParentBoard.pObject, "Board");
         PlaceHolderBoard.pPosY = 60;
         PlaceHolderBoard.pPosX = 10;

      }



   }
}
